//
//  HJDatePickerView.m
//  AINursing
//
//  Created by 黄靖 on 16/2/25.
//  Copyright © 2016年 黄靖. All rights reserved.
//

#import "HJDatePickerView.h"
#define BEGAIN_RECT CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT/3 + 70)       //开始的位置
#define END_RECT CGRectMake(0, SCREEN_HEIGHT/3*2 - 70, SCREEN_WIDTH, SCREEN_HEIGHT/3 + 70)       //结束的位置

@implementation HJDatePickerView
- (instancetype)init
{
    self = [super init];
    if (self) {
        
        // 背景视图
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        self.backgroundColor = [UIColor colorWithHue:0.1 saturation:0.1 brightness:0.1 alpha:0.5];
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelSetting:)];
        tap.numberOfTapsRequired = 1;
        tap.numberOfTouchesRequired = 1;
        [self addGestureRecognizer:tap];
        [self addSubview:self.superView];
        [self.window addSubview:self];
        [self pickerViewAppear];
    }
    return self;
}

// 选择器父视图
- (UIView *)superView{
    if (!_superView) {
        _superView = [[UIView alloc]init];
        _superView.frame = BEGAIN_RECT;
        [_superView addSubview:self.datePicker];
        [_superView addSubview:self.okButton];
    }
    return _superView;
}

// 当前窗口
- (UIWindow *)window{
    if (!_window) {
        _window = [UIApplication sharedApplication].keyWindow;
    }
    return _window;
}

- (void)createDatePickerMode: (UIDatePickerMode) PickMode GetTime:(GetTimeBlock)getTime{
    // 日期选择器
    _datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(10, SCREEN_HEIGHT/3*2-70, SCREEN_WIDTH-20, SCREEN_HEIGHT/3)];
    _datePicker.alpha = 1;
    _datePicker.clipsToBounds = YES;
    _datePicker.layer.cornerRadius = 5;
    _datePicker.backgroundColor = [UIColor whiteColor];
    //       _datePicker.maximumDate = [NSDate date];
    _datePicker.datePickerMode = PickMode;
    [self addSubview:_datePicker];
    self.changeAreaBlock = getTime;
    
    // 弹出窗口确定按钮
    _okButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _okButton.frame = CGRectMake(10, SCREEN_HEIGHT-60, SCREEN_WIDTH-20, 50);
    [_okButton setTitle:@"好的" forState:UIControlStateNormal];
    [_okButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    _okButton.backgroundColor = [UIColor whiteColor];
    _okButton.layer.cornerRadius = 5;
    _okButton.alpha = 1;
    _okButton.titleLabel.font = [UIFont systemFontOfSize:20];
    [_okButton addTarget:self action:@selector(okAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_okButton];
    
}
- (void)okAction:(UIButton *)button{
    
    NSDateFormatter * dfm = [[NSDateFormatter alloc]init];
    [dfm setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString * actionTime = [dfm stringFromDate:_datePicker.date];
    self.changeAreaBlock(actionTime);
    [self removeFromSuperview];
}
- (void)pickerViewAppear{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.3];
    self.superView.frame = END_RECT;
    [UIView commitAnimations];
}

- (void)cancelSetting:(UIGestureRecognizer *)tap{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDidStopSelector:@selector(removeSelfFromSuperView)];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.3];
    self.superView.frame = BEGAIN_RECT;
    [UIView commitAnimations];
    
}
// 将自己从父视图移除
- (void)removeSelfFromSuperView{
    [self removeFromSuperview];
}@end
